<?php

class Sent_item_model extends MY_Model
{

	public function __construct()
	{
		parent::__construct();
	}
	
	public function head()
	{
		return $this->db
			->select('SQL_CALC_FOUND_ROWS si.*', FALSE)
			->from('sent_item AS si');
	}	

	public function add($data = array())
	{
		$this->db->insert('sent_item', $data);
	}

	public function add_batch($data)
	{
		$this->db->insert_batch('sent_item', $data);
	}

	public function get($options = array())
	{

		$this->db->select('*');
		$this->db->from($this->_table);

		// WHERE
		parent::_where($options);

		$query = $this->db->get();

		if (!$query->num_rows())
			return false;

		return $query->row();

	}

	public function gets($options = array(), $limit = 0, $offset = 0)
	{
		$this
			->head()
			->order_by('si.id', 'desc');
			
		// WHERE
		if(isset($options['sent_id']))
			$this->db->where('si.sent_id', $options['sent_id']);
		
		return parent::my_result($limit, $offset);
	}

	public function edit($data = array(), $options = array())
	{

		$this->db->update($this->_table, $data, $options);
	}

	/**
	 * 3 tabloyu birleştirip döndürür.
	 * Genelde main/unsubscribe sayfasında kullanılır.
	 *
	 * @param int
	 * @return object
	 */
	public function get_more($id)
	{

		$q = $this->db->query("
			SELECT si.id, si.sent_id, si.address, s.user_id
            FROM {$this->db->dbprefix('sent_item')} AS si
            JOIN {$this->db->dbprefix('sent')} AS s ON (si.sent_id = s.id)
            WHERE si.id = {$id}");

		return $q->row();
	}

	/**
	 * Verilen kriterlere uygun bir kayıt olup olmadığını kontrol eder.
	 *
	 * @param array $options
	 * @return bool
	 */
	public function has($options = array())
	{

		$this->db->from($this->_table);

		// WHERE
		parent::_where($options);

		return $this->db->count_all_results();
	}

}

/* End of file sent_item_model.php */
